﻿using System;
using System.Windows.Forms;
using MediaPortal.DeployTool;
using Microsoft.HomeServer.Controls;

namespace Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn.Wizards.UninstallTVServer
{
    #region Wizard
    public class UninstallTVServerWizard : Wizard
    {
        /// <summary>
        /// Initializes a new instance of the UninstallTVServerWizard class.
        /// </summary>
        public UninstallTVServerWizard()
        {
            base.Width = CommonSettings.BigWizardStdWidth;
            base.Height = CommonSettings.BigWizardStdHeight;
            base.WizardIcon = Properties.Resources.SetupTv;
            base.Text = Resources_UninstallTVServerWizard.InstallTVServerWizard_WizardTitle;
            base.Init();

            // set the available wizard pages
            base.SetTypes(new Type[]
                              {
                                  typeof (WPUninstallStart),
                                  typeof (WPUninstallation),
                                  typeof (WPFinished)
                              });

            base.DialogResult = DialogResult.Cancel;
        }
    }
    #endregion

    #region WizardPages
    class WPUninstallStart : WizardPage
    {
        WPUninstallStartControl pageControl;

        public WPUninstallStart(Wizard parentWizard, object currentSubject, object currentParameter1)
        {
            if (parentWizard == null)
                throw new ArgumentNullException();

            base.ParentWizard = parentWizard;

            base.TitleText = Resources_UninstallTVServerWizard.WPUninstallStart_WizardPage_TitleText;

            base.OkButtonName = CommonSettings.WizardStandardButtonNameNext;
            base.CancelButtonName = CommonSettings.WizardStandardButtonNameCancel;
            base.BackButtonName = CommonSettings.WizardStandardButtonNameBack;

            base.HideTopBannerAndLines = false;

            base.Init();

            base.OkButtonhook(this.OnNext);

            base.OkButtonEnabled = true;
            base.BackButtonEnabled = false;
            base.CancelButtonEnabled = true;

            pageControl = new WPUninstallStartControl();

            base.PlaceControlIntoWizardPage(pageControl);
        }

        public override void StartRealWork()
        {
        }

        private void OnNext(object obj, EventArgs ea)
        {
            base.ParentWizard.CurrentParameter1 = pageControl.StandardUninstall;

            base.ParentWizard.SetCurrentPanel(typeof(WPUninstallation));
        }
    }

    class WPUninstallation : WizardPage
    {
        WPUninstallationControl pageControl;

        public WPUninstallation(Wizard parentWizard, object currentSubject, object currentParameter1)
        {
            if (parentWizard == null)
                throw new ArgumentNullException();

            base.ParentWizard = parentWizard;

            base.TitleText = Resources_UninstallTVServerWizard.WPUninstallation_WizardPage_TitleText;

            base.OkButtonName = Resources_UninstallTVServerWizard.WPUninstallation_WizardPage_NextButtonText; ;
            base.CancelButtonName = CommonSettings.WizardStandardButtonNameCancel;
            base.BackButtonName = CommonSettings.WizardStandardButtonNameBack;

            base.HideTopBannerAndLines = false;

            base.Init();

            base.OkButtonhook(this.OnNext);
            base.BackButtonhook(this.OnBack);

            base.OkButtonEnabled = true;
            base.BackButtonEnabled = true;

            pageControl = new WPUninstallationControl();

            base.PlaceControlIntoWizardPage(pageControl);
        }

        public override void StartRealWork()
        {
        }

        private void OnNext(object obj, EventArgs ea)
        {
            bool uninstallSucceeded = false;

            base.OkButtonEnabled = false;
            base.BackButtonEnabled = false;
            base.CancelButtonEnabled = false;
            base.ParentWizard.PreventClose = true;

            try
            {
                uninstallSucceeded = pageControl.UninstallComponents((bool)base.ParentWizard.CurrentParameter1);
            }
            catch
            {
            }

            base.ParentWizard.PreventClose = false;

            base.ParentWizard.SetCurrentPanel(typeof(WPFinished), !uninstallSucceeded);
        }

        private void OnBack(object sender, EventArgs e)
        {
            base.ParentWizard.SetCurrentPanel(typeof(WPUninstallStart));
        }
    }

    class WPFinished : WizardPage
    {
        Control pageControl;

        public WPFinished(Wizard parentWizard, object currentSubject, object currentParameter1)
        {
            if (parentWizard == null)
                throw new ArgumentNullException();

            base.ParentWizard = parentWizard;

            base.OkButtonName = CommonSettings.WizardStandardButtonNameNext;
            base.BackButtonName = CommonSettings.WizardStandardButtonNameBack;
            base.CancelButtonName = CommonSettings.WizardStandardButtonNameCancel;

            base.HideLeftBanner = false;
            base.HideTopBannerAndLines = true;

            base.Init();

            base.OkButtonVisible = false;
            base.BackButtonVisible = false;

            base.CancelButtonName = CommonSettings.WizardStandardButtonNameDone;
            base.CancelButton.Text = CommonSettings.WizardStandardButtonNameDone;
            base.CancelButton.DialogResult = DialogResult.OK;

            base.CancelButtonEnabled = true;
            base.CancelButton.Select();

            base.HideTopBannerAndLines = false;

            base.CancelButtonhook(this.OnNext);

            if (base.IsAtError)
                pageControl = new WPFinishedErrorControl();
            else
                pageControl = new WPFinishedControl();

            base.PlaceControlIntoWizardPage(pageControl);
        }

        public override void StartRealWork()
        {
        }

        private void OnNext(object obj, EventArgs ea)
        {
            // wizard comleted
            base.ParentWizard.DialogResult = DialogResult.OK;
            base.Invalidate();
        }
    }
    #endregion
}
